//
//  SwiftUIView_Binding.swift
//  SwiftUIPractice
//
//  Created by 绑定绑定又是TMD绑定 (external - Project) on 2024/9/12.
//

import SwiftUI

struct SwiftUIView_Binding: View {
    
    @State var backgroundColor = Color.green
    
    @State var txt = "原始标题"
    
    var body: some View {

        ZStack {
            
            backgroundColor
        
            VStack {

                Text("\(txt)")
                
                MyButton(backgroundColor_A: $backgroundColor, txt: $txt)

            }
        }
    }
}


// 这里想要做的事情是, 在子视图里面去点击按钮, 然后要去改变父视图的颜色, 那么这种 "向上传递" 的东西要怎么去做
struct MyButton: View {

    @Binding var backgroundColor_A: Color  //这个属性的命名可以与父视图不同, 但是在传入参数的时 候, 找到对应的参数就行了

    @State var buttonColor = Color.yellow
    
    @Binding var txt: String
    
    var body: some View {

        Button(action: {
            backgroundColor_A = .orange
            buttonColor = .pink
            txt = "按钮点击了"
        }, label: {
            Text("Button").foregroundStyle(.white).padding().padding(.horizontal).background(buttonColor).cornerRadius(10)
        })
        
    }
}



#Preview {
    SwiftUIView_Binding()
}
